package com.dayu.recovery.service.impl;

import java.util.List;
import com.dayu.common.utils.DateUtils;
import com.dayu.common.utils.SecurityUtils;
import com.dayu.recovery.service.IRecoveryGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayu.recovery.mapper.RecoveryCollectMapper;
import com.dayu.recovery.domain.RecoveryCollect;
import com.dayu.recovery.service.IRecoveryCollectService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 回收管理Service业务层处理
 *
 * @author HK
 * @date 2021-04-16
 */
@Service
public class RecoveryCollectServiceImpl implements IRecoveryCollectService
{
    @Autowired
    private RecoveryCollectMapper recoveryCollectMapper;
    @Autowired
    private IRecoveryGoodsService goodsService;

    /**
     * 查询回收管理
     *
     * @param collectId 回收管理ID
     * @return 回收管理
     */
    @Override
    public RecoveryCollect selectRecoveryCollectById(Long collectId)
    {
        return recoveryCollectMapper.selectRecoveryCollectById(collectId);
    }

    /**
     * 查询回收管理列表
     *
     * @param recoveryCollect 回收管理
     * @return 回收管理
     */
    @Override
    public List<RecoveryCollect> selectRecoveryCollectList(RecoveryCollect recoveryCollect)
    {
        return recoveryCollectMapper.selectRecoveryCollectList(recoveryCollect);
    }

    @Override
    public Long selectCollectNumber(RecoveryCollect recoveryCollect) {
        List<RecoveryCollect> list = selectRecoveryCollectList(recoveryCollect);
        Long res = 0L;
        for (RecoveryCollect collect:
             list) {
            res += collect.getCollectNum();
        }
        return res;
    }

    /**
     * 新增回收管理
     *
     * @param recoveryCollect 回收管理
     * @return 结果
     */
    @Override
    public int insertRecoveryCollect(RecoveryCollect recoveryCollect)
    {
        if("y".equals(recoveryCollect.getHasGreen())) {
            if(ObjectUtils.isEmpty(recoveryCollect.getCollectPrice())) {
                throw new RuntimeException("请输入价格！");
            }
            String[] arr = recoveryCollect.getCollectPrice().split(",");
            if(arr.length > 1) {
                throw new RuntimeException("绿色回收只需要一个总价！");
            }
        }
        recoveryCollect.setCreateId(SecurityUtils.getLoginUser().getUser().getUserId());
        recoveryCollect.setCreateTime(DateUtils.getNowDate());
        return recoveryCollectMapper.insertRecoveryCollect(recoveryCollect);
    }

    /**
     * 修改回收管理
     *
     * @param recoveryCollect 回收管理
     * @return 结果
     */
    @Override
    public int updateRecoveryCollect(RecoveryCollect recoveryCollect)
    {
        return recoveryCollectMapper.updateRecoveryCollect(recoveryCollect);
    }

    @Override
    @Transactional
    public int generateRecoveryGoods(RecoveryCollect r) {
        if(ObjectUtils.isEmpty(r.getCollectId())) {
            throw new RuntimeException("收货id不能为空！");
        }
        //修改回收参数
        RecoveryCollect collect = selectRecoveryCollectById(r.getCollectId());
        collect.setStatus(2);
        updateRecoveryCollect(collect);
        return goodsService.insertRecoveryGoodsByList(collect);
    }

    /**
     * 批量删除回收管理
     *
     * @param collectIds 需要删除的回收管理ID
     * @return 结果
     */
    @Override
    public int deleteRecoveryCollectByIds(Long[] collectIds)
    {
        return recoveryCollectMapper.deleteRecoveryCollectByIds(collectIds);
    }

    /**
     * 删除回收管理信息
     *
     * @param collectId 回收管理ID
     * @return 结果
     */
    @Override
    public int deleteRecoveryCollectById(Long collectId)
    {
        return recoveryCollectMapper.deleteRecoveryCollectById(collectId);
    }
}
